package org.system.core.service.system;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.system.core.domain.entity.SysPost;
import org.system.core.exception.ServiceException;
import org.system.core.mapper.system.SysPostMapper;
import org.system.core.mapper.system.SysUserPostMapper;
import org.system.core.utils.MessageUtils;
import org.system.core.utils.SecurityUtils;
import org.system.core.utils.StringUtils;

import java.util.List;

/**
 * @author baoRoot
 */
@Service
public class SysPostService {

    @Autowired
    private SysPostMapper sysPostMapper;

    @Autowired
    private SysUserPostMapper sysUserPostMapper;

    public List<SysPost> selectPostAll() {
        return sysPostMapper.selectPostAll();
    }

    public List<Long> selectPostListByUserId(Long userId) {
        return sysPostMapper.selectPostListByUserId(userId);
    }

    public List<SysPost> selectPostList(Long userId) {
        return sysPostMapper.selectPostLists(userId);
    }

    public List<SysPost> selectPostList(SysPost post) {
        return sysPostMapper.selectPostList(post);
    }

    public SysPost selectPostById(Long postId) {
        if(postId == null) {
            throw new ServiceException(MessageUtils.message("system.post.no_id"));
        }
        return sysPostMapper.selectPostById(postId);
    }

    public boolean checkPostCodeUnique(SysPost post) {
        Long postId = StringUtils.isNull(post.getPostId()) ? -1L : post.getPostId();
        SysPost info = sysPostMapper.checkPostCodeUnique(post.getPostCode());
        return !StringUtils.isNotNull(info) || info.getPostId().longValue() == postId.longValue();
    }

    public boolean checkPostNameUnique(SysPost post) {
        Long postId = StringUtils.isNull(post.getPostId()) ? -1L : post.getPostId();
        SysPost info = sysPostMapper.checkPostNameUnique(post.getPostName());
        return !StringUtils.isNotNull(info) || info.getPostId().longValue() == postId.longValue();
    }

    public void beforeVerify(SysPost post) {
        if(!checkPostNameUnique(post)) {
            throw new ServiceException(MessageUtils.message("system.post.post_name_exist"));
        }
        if (!checkPostCodeUnique(post)) {
            throw new ServiceException(MessageUtils.message("system.post.post_code_exist"));
        }
    }

    public void insertPost(SysPost post) {
        beforeVerify(post);
        post.setCreateBy(SecurityUtils.getUsername());
        if(sysPostMapper.insertPost(post) <= 0) {
            throw new ServiceException(MessageUtils.message("system.monitor.update_error"));
        }
    }

    public void updatePost(SysPost post) {
        beforeVerify(post);
        post.setUpdateBy(SecurityUtils.getUsername());
        if(sysPostMapper.updatePost(post) <= 0) {
            throw new ServiceException(MessageUtils.message("system.monitor.update_error"));
        }
    }

    public int countUserPostById(Long postId) {
        return sysUserPostMapper.countUserPostById(postId);
    }

    public void deletePost(Long[] postIds) {
        for (Long postId : postIds) {
            SysPost post = selectPostById(postId);
            if (countUserPostById(postId) > 0) {
                throw new ServiceException(String.format("%1$s已分配,不能删除", post.getPostName()));
            }
        }
        if(sysPostMapper.deletePostByIds(postIds) <= 0) {
            throw new ServiceException(MessageUtils.message("system.monitor.delete_error"));
        }
    }

}
